package com.ggh.jinjilive.ui.social;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager;

import com.amap.api.services.core.LatLonPoint;
import com.blankj.utilcode.util.BarUtils;
import com.ggh.jinjilive.R;
import com.ggh.jinjilive.databinding.FragmentSocialBinding;
import com.ggh.jinjilive.listener.AddItemOnClickListener;
import com.ggh.jinjilive.mobile.TabEntity;
import com.ggh.jinjilive.ui.BaseFragment;
import com.ggh.jinjilive.ui.map.NearMapActivity;
import com.ggh.jinjilive.ui.social.message.MessageFragment;
import com.ggh.jinjilive.ui.social.near.NearLiveFragment;
import com.ggh.jinjilive.ui.social.near.NearUserFragment;
import com.ggh.jinjilive.ui.social.record.RecordFragment;
import com.ggh.jinjilive.utils.LocationUtils;
import com.flyco.tablayout.listener.CustomTabEntity;
import com.flyco.tablayout.listener.OnTabSelectListener;
import com.tencent.imsdk.v2.V2TIMManager;
import com.tencent.imsdk.v2.V2TIMValueCallback;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;

public class SocialFragment extends BaseFragment<FragmentSocialBinding> implements View.OnClickListener {
    private String[] mTitles = {"附件用户", "消息", "附件直播", "访客记录"};
    private int[] mIconUnselectIds = {
            R.mipmap.ic_near_user, R.mipmap.ic_message,
            R.mipmap.ic_near_live, R.mipmap.ic_record};
    private int[] mIconSelectIds = {
            R.mipmap.ic_near_user_pitchon, R.mipmap.ic_message_pitchon,
            R.mipmap.ic_near_live_pitchon, R.mipmap.ic_record_pitchon};

    private ArrayList<CustomTabEntity> mTabEntities = new ArrayList<>();
    //
    private List<Fragment> fragmentList = new ArrayList<>();
    //附近用户
    private NearUserFragment nearUserFragment;
    //消息
    private MessageFragment messageFragment;
    //附近直播
    private NearLiveFragment nearLiveFragment;
    //访客记录
    private RecordFragment recordFragment;
    //适配器
    private ProfitOrderAdapter profitOrderAdapter;
    //经纬度
    private LatLonPoint latLonPoint;
    @Override
    public void onCreate(@Nullable @org.jetbrains.annotations.Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public int initContentView(LayoutInflater inflater, @Nullable @org.jetbrains.annotations.Nullable ViewGroup container, @Nullable @org.jetbrains.annotations.Nullable Bundle savedInstanceState) {
        return R.layout.fragment_social;
    }

    @Override
    public void initView() {
        super.initView();
        //获取状态栏高度
        binding.llSpacing.setPadding(0, BarUtils.getStatusBarHeight(),0,0);
        fragmentList.clear();
        //附近用户
        nearUserFragment = new NearUserFragment();
        fragmentList.add(nearUserFragment);
        //消息
        messageFragment = new MessageFragment();
        fragmentList.add(messageFragment);
        //附近直播
        nearLiveFragment = new NearLiveFragment();
        fragmentList.add(nearLiveFragment);
        //访客记录
        recordFragment = new RecordFragment();
        fragmentList.add(recordFragment);

        mTabEntities.clear();
        for (int i = 0; i < mTitles.length; i++) {
            mTabEntities.add(new TabEntity(mTitles[i], mIconSelectIds[i], mIconUnselectIds[i]));
        }
        binding.tl2.setTabData(mTabEntities);

        //适配器
        profitOrderAdapter = new ProfitOrderAdapter(getChildFragmentManager());
        binding.vp.setAdapter(profitOrderAdapter);
        //未读数量
        messageFragment.setAddItemOnClickListener(new AddItemOnClickListener() {
            @Override
            public void onSkipClicked(int position, Object mObject) {
                binding.tl2.showMsg(1,position);
            }
        });
        //未读数量
        V2TIMManager.getConversationManager().getTotalUnreadMessageCount(new V2TIMValueCallback<Long>() {
            @Override
            public void onSuccess(Long aLong) {
                binding.tl2.showMsg(1,aLong.intValue());
            }

            @Override
            public void onError(int code, String desc) {

            }
        });
    }

    @Override
    public void initData() {
        super.initData();
        //显示位置信息
        binding.tvNearMap.setText(userMobile.getCity());
        //
        binding.tl2.setOnTabSelectListener(new OnTabSelectListener() {
            @Override
            public void onTabSelect(int position) {

                binding.vp.setCurrentItem(position);
            }

            @Override
            public void onTabReselect(int position) {

            }
        });
        binding.vp.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {

                binding.tl2.setCurrentTab(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    @Override
    public void initViewObservable() {
        super.initViewObservable();
        //确认
        binding.tvNearMap.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.tv_near_map:
                Intent intent = new Intent(getContext(), NearMapActivity.class);
                intent.putExtra("latLonPoint",latLonPoint);
                startActivityForResult(intent,2011);
                break;
        }
    }


    private class ProfitOrderAdapter extends FragmentPagerAdapter {
        public ProfitOrderAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            return fragmentList.get(position);
        }

        @Override
        public int getCount() {
            return fragmentList.size();
        }

        @Override
        public void destroyItem(@NonNull @NotNull ViewGroup container, int position, @NonNull @NotNull Object object) {
//            super.destroyItem(container, position, object);
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return mTitles[position];
        }

        @Override
        public void setPrimaryItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
            super.setPrimaryItem(container, position, object);
        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, @Nullable @org.jetbrains.annotations.Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(resultCode == 2011){
            latLonPoint = data.getParcelableExtra("latLonPoint");
            if(null != latLonPoint){
                //
                LocationUtils.GeocodeSearch(binding.tvNearMap,latLonPoint);
                //
                nearUserFragment.setLatLonPoint(latLonPoint);

            }
        }
    }
}
